<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
  </head>
  <body></body>
  <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.13.42/dingtalk.open.js"></script>
  <script>
    const getQueryParameters = () => {
      const url = window.location.href
      const search = url.split("?")[1]
      if (!search) {
        return {}
      }
      return JSON.parse(
        '{"' +
          decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
          '"}'
      )
    }
    const params = getQueryParameters()
    if (params.corpId) {
      dd.ready(function () {
        dd.runtime.permission.requestAuthCode({
          corpId: params.corpId, // 企业id
          onSuccess: function (info) {
            fetch(
              `${window.location.origin}/api/plugin/dingtalk/login?code=${info.code}`
            )
              .then((res) => {
                return res.json()
              })
              .then((res) => {
                if (res && res.code === 0 && res.data) {
                  window.localStorage.setItem("App-Token", res.data)
                  window.location.href = "./index.html"
                } else {
                  alert(res ? res.message : "登录失败")
                }
              })
              .catch((err) => {
                alert("登录失败")
              })
          },
        })
      })
    } else {
      alert("请检查PC端首页地址配置")
      // window.location.href = "./index.html"
    }
  </script>
</html>
